package com.cn.springcloudwechat.common.utils.reptile;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class JsoupTest {
    public static void main(String[] args) throws IOException {
        Connection connect = Jsoup.connect("http://www.360doc.com/content/17/0110/16/4087358_621549823.shtml");
        Document document = connect.get();
        table1(document);
    }

    public static void down(String filePath, String imgUrl) throws IOException {
        File dir = new File(filePath);
        if (!dir.getParentFile().exists()) {
            dir.getParentFile().mkdirs();
        }
        // 截取图片文件名
        String fileName = imgUrl.substring(imgUrl.lastIndexOf('/') + 1, imgUrl.length());
        String encode = URLEncoder.encode(fileName, "UTF-8");
        imgUrl = imgUrl.substring(0, imgUrl.lastIndexOf('/') + 1) + encode.replaceAll("\\+", "\\%20");
        File file = new File(filePath + File.separator + fileName);
        URL url = new URL(imgUrl);
        URLConnection urlConnection = url.openConnection();
        urlConnection.setConnectTimeout(10 * 1000);
        InputStream in = urlConnection.getInputStream();
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        byte[] bytes = new byte[1024];
        int size;
        while (-1 != (size = in.read(bytes))) {
            out.write(bytes, 0, size);
        }
        out.close();
        in.close();

    }

    /**
     * table
     *
     * @param doc
     * @return
     */
    private static int table1(Document doc) {
        Elements trs = doc.select("table").get(1).select("tr");
        int i;
        for (i = 1; i < trs.size(); i++) {
            Elements tds = trs.get(i).select("td");
            for (int j = 0; j < tds.size(); j++) {
                String td = tds.get(j).text();
                System.out.print(td + " ");
            }
            System.out.println("");
        }
        return i;
    }

    /**
     * img
     *
     * @param document
     * @throws IOException
     */
    public static void img(Document document) throws IOException {
        Elements img = document.getElementsByTag("img");
        for (Element elements : img) {
            String attr = elements.attr("abs:src");
            if (attr.toLowerCase().contains("jpg") || attr.toLowerCase().contains("png")) {
                System.out.println(attr);
                down("d:/img", attr);
            }
        }
    }
}
